package io.ionic.starter.keep;

import android.app.ActivityManager;
import android.content.Context;

import java.util.List;

/**工具类*/
public class SystemUtils {

    /**
     * 判断本应用是否存活
     * 如果需要判断本应用是否在后台还是前台用getRunningTask
     * */
    public static boolean isAPPALive(Context mContext, String packageName){
        boolean isAPPRunning = false;
        ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcessInfoList = activityManager.getRunningAppProcesses();
        for(ActivityManager.RunningAppProcessInfo appInfo : appProcessInfoList){
            if(packageName.equals(appInfo.processName)){
                isAPPRunning = true;
                break;
            }
        }
        return isAPPRunning;
    }

  public static boolean isServiceRunning(Context context, String serviceName) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningServiceInfo> runningServiceInfos = am.getRunningServices(200);
    if (runningServiceInfos.size() <= 0) {
      return false;
    }
    for (ActivityManager.RunningServiceInfo serviceInfo : runningServiceInfos) {
      if (serviceInfo.service.getClassName().equals(serviceName)) {
        return true;
      }
    }
    return false;
  }
}
